package wiki.hitime.stl.daq.mapper;

import wiki.hitime.stl.daq.bean.db.DaqExecuteRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

import java.util.List;

/**
 * 执行任务记录表(DaqExecuteRecord)表数据库访问层
 *
 * @author zhangdp
 * @since 2019-07-15 16:59:58
 */
@Repository
public interface DaqExecuteRecordMapper extends Mapper<DaqExecuteRecord>, MySqlMapper<DaqExecuteRecord> {


    /**
     * 获取规则对应最大记录
     *
     * @param executeRuleIdList 执行规则编号集合
     * @return
     */
    @Select("<script>" +
            "SELECT " +
            "  `id`, `execute_rule_id`, `rule_name`, `cron_expression`, `start_time`, `end_time`, `remark`, `process_status`," +
            "  `process_msg`, `ctl_version`, `is_delete`, `modified`, `created` " +
            "FROM daq_execute_record a " +
            "<where>" +
            "1 > (SELECT count(*) " +
            "           FROM daq_execute_record " +
            "<where>" +
            "  execute_rule_id = a.execute_rule_id AND id > a.id) " +
            "<if test='list!=null and list.size() > 0'>" +
            "AND execute_rule_id IN " +
            "<foreach collection='list' item='item' separator=',' open='(' close=')'>" +
            "   #{item}" +
            "</foreach>" +
            "</if>" +
            "</where>" +
            "</where>" +
            "ORDER BY a.execute_rule_id DESC, a.id DESC" +
            "</script>")
    List<DaqExecuteRecord> listMax(@Param("list") List<Long> executeRuleIdList);

}
